<template>
  <div class="rank-container">
    <div class="rank-chart" ref="chartRef"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Rank',
  data() {
    return {
      chartInstance: null,
      allData: null,
      timer: null,
      startValue: 0, // 区域缩放的起始值
      endValue: 9, // 终点值
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // 主题改变时，销毁当前图表
      this.chartInstance.dispose()
      // 以最新的主题初始化图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  created() {
    this.$socket.registerCallBack('rankData', this.getData)
  },
  mounted() {
    this.initChart()
    //this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: ''
     })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize',this.screenAdapter)
    clearInterval(this.timer)
    this.$socket.unRegisterCallBack('rankData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chartRef, this.theme)
      const initOption = {
        title: {
          text: '┃ 地区销售排行',
          top: 20,
          left: 20
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ]
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseout',() => { 
        this.startInterval()
      })
      this.chartInstance.on('mouseover',() => {
        clearInterval(this.timer)
      })
    },

    getData(result) {
      //const result = await this.$http.get('rank')
      this.allData = result
      this.allData.sort((a,b) => {
        return b.value - a.value
      })
      this.updateChart()
      this.startInterval()
    },

    updateChart() {
      const colorArr = [
        ['#0ba82c','#4ff778'],
        ['#2e728f','#23e5e5'],
        ['#5052ee','#ab6ee5']
      ]
      // x轴
      const provinceArr = this.allData.map(item => {
        return item.name
      })
      // y轴
      const valueArr = this.allData.map(item => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                let currentColorArr = null
                if(arg.value > 300) {
                  currentColorArr = colorArr[0]
                } else if(arg.value > 200) {
                  currentColorArr = colorArr[1]
                } else {
                  currentColorArr = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0,0,0,1,[
                  {
                    offset: 0,
                    color: currentColorArr[0]
                  },
                  {
                    offset: 1,
                    color: currentColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.chartInstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter() {
      const titleFontSize = this.$refs.chartRef.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 定时器
    startInterval() {
      if(this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.startValue++
        this.endValue++
        if(this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.updateChart()
      }, 2000)
    }
  },
}
</script>

<style scoped>

</style>